home *** CD-ROM | disk | FTP | other *** search
/ Team Palmtops 7 / Palmtops_numero07.iso / WinCE / SDKWindowsCE / HandHeldPCPro30 / sdk.exe / Jupiter SDK / data1.cab / MFC / src / filex.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-02-19  |  9.1 KB  |  327 lines

  1. // This is a part of the Microsoft Foundation Classes C++ library.
  2. // Copyright (C) 1992-1998 Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Microsoft Foundation Classes Reference and related
  7. // electronic documentation provided with the library.
  8. // See these sources for detailed information regarding the
  9. // Microsoft Foundation Classes product.
  10.  
  11. #include "stdafx.h"
  12. #ifndef _INC_ERRNO
  13. #include <errno.h>
  14. #endif
  15.  
  16. #ifdef AFX_CORE1_SEG
  17. #pragma code_seg(AFX_CORE1_SEG)
  18. #endif
  19.  
  20. #ifdef _DEBUG
  21. #undef THIS_FILE
  22. static char THIS_FILE[] = __FILE__;
  23. #endif
  24.  
  25. #ifdef _DEBUG
  26. static const LPCSTR rgszCFileExceptionCause[] =
  27. {
  28.     "none",
  29.     "generic",
  30.     "fileNotFound",
  31.     "badPath",
  32.     "tooManyOpenFiles",
  33.     "accessDenied",
  34.     "invalidFile",
  35.     "removeCurrentDir",
  36.     "directoryFull",
  37.     "badSeek",
  38.     "hardIO",
  39.     "sharingViolation",
  40.     "lockViolation",
  41.     "diskFull",
  42.     "endOfFile",
  43. };
  44. static const char szUnknown[] = "unknown";
  45. #endif
  46.  
  47. /////////////////////////////////////////////////////////////////////////////
  48. // CFileException
  49.  
  50. void PASCAL CFileException::ThrowOsError(LONG lOsError,
  51.     LPCTSTR lpszFileName /* = NULL */)
  52. {
  53.     if (lOsError != 0)
  54.         AfxThrowFileException(CFileException::OsErrorToException(lOsError),
  55.             lOsError, lpszFileName);
  56. }
  57.  
  58. #if !defined(_WIN32_WCE)
  59. void PASCAL CFileException::ThrowErrno(int nErrno,
  60.     LPCTSTR lpszFileName /* = NULL */)
  61. {
  62.     if (nErrno != 0)
  63.         AfxThrowFileException(CFileException::ErrnoToException(nErrno),
  64.             _doserrno, lpszFileName);
  65. }
  66. #endif // _WIN32_WCE
  67.  
  68. BOOL CFileException::GetErrorMessage(LPTSTR lpszError, UINT nMaxError,
  69.     PUINT pnHelpContext)
  70. {
  71.     ASSERT(lpszError != NULL && AfxIsValidString(lpszError, nMaxError));
  72.  
  73.     if (pnHelpContext != NULL)
  74.         *pnHelpContext = m_cause + AFX_IDP_FILE_NONE;
  75.  
  76.     CString strMessage;
  77.     CString strFileName = m_strFileName;
  78.     if (strFileName.IsEmpty())
  79.         strFileName.LoadString(AFX_IDS_UNNAMED_FILE);
  80.     AfxFormatString1(strMessage,
  81.         m_cause + AFX_IDP_FILE_NONE, strFileName);
  82.     lstrcpyn(lpszError, strMessage, nMaxError);
  83.  
  84.     return TRUE;
  85. }
  86.  
  87. /////////////////////////////////////////////////////////////////////////////
  88. // CFileException diagnostics
  89.  
  90. #ifdef _DEBUG
  91. void CFileException::Dump(CDumpContext& dc) const
  92. {
  93.     CObject::Dump(dc);
  94.  
  95.     dc << "m_cause = ";
  96.     if (m_cause >= 0 && m_cause < _countof(rgszCFileExceptionCause))
  97.         dc << rgszCFileExceptionCause[m_cause];
  98.     else
  99.         dc << szUnknown;
  100.     dc << "\nm_lOsError = " << (void*)m_lOsError;
  101.  
  102.     dc << "\n";
  103. }
  104. #endif
  105.  
  106. /////////////////////////////////////////////////////////////////////////////
  107. // CFileException helpers
  108.  
  109. void AFXAPI AfxThrowFileException(int cause, LONG lOsError,
  110.     LPCTSTR lpszFileName /* == NULL */)
  111. {
  112. #ifdef _DEBUG
  113.     LPCSTR lpsz;
  114.     if (cause >= 0 && cause < _countof(rgszCFileExceptionCause))
  115.         lpsz = rgszCFileExceptionCause[cause];
  116.     else
  117.         lpsz = szUnknown;
  118.     TRACE3("CFile exception: %hs, File %s, OS error information = %ld.\n",
  119.         lpsz, (lpszFileName == NULL) ? _T("Unknown") : lpszFileName, lOsError);
  120. #endif
  121.     THROW(new CFileException(cause, lOsError, lpszFileName));
  122. }
  123.  
  124. #if !defined(_WIN32_WCE)
  125. int PASCAL CFileException::ErrnoToException(int nErrno)
  126. {
  127.     switch(nErrno)
  128.     {
  129.     case EPERM:
  130.     case EACCES:
  131.         return CFileException::accessDenied;
  132.     case EBADF:
  133.         return CFileException::invalidFile;
  134.     case EDEADLOCK:
  135.         return CFileException::sharingViolation;
  136.     case EMFILE:
  137.         return CFileException::tooManyOpenFiles;
  138.     case ENOENT:
  139.     case ENFILE:
  140.         return CFileException::fileNotFound;
  141.     case ENOSPC:
  142.         return CFileException::diskFull;
  143.     case EINVAL:
  144.     case EIO:
  145.         return CFileException::hardIO;
  146.     default:
  147.         return CFileException::generic;
  148.     }
  149. }
  150. #endif // _WIN32_WCE
  151.  
  152. int PASCAL CFileException::OsErrorToException(LONG lOsErr)
  153. {
  154.     // NT Error codes
  155.     switch ((UINT)lOsErr)
  156.     {
  157.     case NO_ERROR:
  158.         return CFileException::none;
  159.     case ERROR_FILE_NOT_FOUND:
  160.         return CFileException::fileNotFound;
  161.     case ERROR_PATH_NOT_FOUND:
  162.         return CFileException::badPath;
  163.     case ERROR_TOO_MANY_OPEN_FILES:
  164.         return CFileException::tooManyOpenFiles;
  165.     case ERROR_ACCESS_DENIED:
  166.         return CFileException::accessDenied;
  167.     case ERROR_INVALID_HANDLE:
  168.         return CFileException::fileNotFound;
  169.     case ERROR_BAD_FORMAT:
  170.         return CFileException::invalidFile;
  171.     case ERROR_INVALID_ACCESS:
  172.         return CFileException::accessDenied;
  173.     case ERROR_INVALID_DRIVE:
  174.         return CFileException::badPath;
  175.     case ERROR_CURRENT_DIRECTORY:
  176.         return CFileException::removeCurrentDir;
  177.     case ERROR_NOT_SAME_DEVICE:
  178.         return CFileException::badPath;
  179.     case ERROR_NO_MORE_FILES:
  180.         return CFileException::fileNotFound;
  181.     case ERROR_WRITE_PROTECT:
  182.         return CFileException::accessDenied;
  183.     case ERROR_BAD_UNIT:
  184.         return CFileException::hardIO;
  185.     case ERROR_NOT_READY:
  186.         return CFileException::hardIO;
  187.     case ERROR_BAD_COMMAND:
  188.         return CFileException::hardIO;
  189.     case ERROR_CRC:
  190.         return CFileException::hardIO;
  191.     case ERROR_BAD_LENGTH:
  192.         return CFileException::badSeek;
  193.     case ERROR_SEEK:
  194.         return CFileException::badSeek;
  195.     case ERROR_NOT_DOS_DISK:
  196.         return CFileException::invalidFile;
  197.     case ERROR_SECTOR_NOT_FOUND:
  198.         return CFileException::badSeek;
  199.     case ERROR_WRITE_FAULT:
  200.         return CFileException::accessDenied;
  201.     case ERROR_READ_FAULT:
  202.         return CFileException::badSeek;
  203.     case ERROR_SHARING_VIOLATION:
  204.         return CFileException::sharingViolation;
  205.     case ERROR_LOCK_VIOLATION:
  206.         return CFileException::lockViolation;
  207.     case ERROR_WRONG_DISK:
  208.         return CFileException::badPath;
  209.     case ERROR_SHARING_BUFFER_EXCEEDED:
  210.         return CFileException::tooManyOpenFiles;
  211.     case ERROR_HANDLE_EOF:
  212.         return CFileException::endOfFile;
  213.     case ERROR_HANDLE_DISK_FULL:
  214.         return CFileException::diskFull;
  215.     case ERROR_DUP_NAME:
  216.         return CFileException::badPath;
  217.     case ERROR_BAD_NETPATH:
  218.         return CFileException::badPath;
  219.     case ERROR_NETWORK_BUSY:
  220.         return CFileException::accessDenied;
  221.     case ERROR_DEV_NOT_EXIST:
  222.         return CFileException::badPath;
  223.     case ERROR_ADAP_HDW_ERR:
  224.         return CFileException::hardIO;
  225.     case ERROR_BAD_NET_RESP:
  226.         return CFileException::accessDenied;
  227.     case ERROR_UNEXP_NET_ERR:
  228.         return CFileException::hardIO;
  229.     case ERROR_BAD_REM_ADAP:
  230.         return CFileException::invalidFile;
  231.     case ERROR_NO_SPOOL_SPACE:
  232.         return CFileException::directoryFull;
  233.     case ERROR_NETNAME_DELETED:
  234.         return CFileException::accessDenied;
  235.     case ERROR_NETWORK_ACCESS_DENIED:
  236.         return CFileException::accessDenied;
  237.     case ERROR_BAD_DEV_TYPE:
  238.         return CFileException::invalidFile;
  239.     case ERROR_BAD_NET_NAME:
  240.         return CFileException::badPath;
  241.     case ERROR_TOO_MANY_NAMES:
  242.         return CFileException::tooManyOpenFiles;
  243.     case ERROR_SHARING_PAUSED:
  244.         return CFileException::badPath;
  245.     case ERROR_REQ_NOT_ACCEP:
  246.         return CFileException::accessDenied;
  247.     case ERROR_FILE_EXISTS:
  248.         return CFileException::accessDenied;
  249.     case ERROR_CANNOT_MAKE:
  250.         return CFileException::accessDenied;
  251.     case ERROR_ALREADY_ASSIGNED:
  252.         return CFileException::badPath;
  253.     case ERROR_INVALID_PASSWORD:
  254.         return CFileException::accessDenied;
  255.     case ERROR_NET_WRITE_FAULT:
  256.         return CFileException::hardIO;
  257.     case ERROR_DISK_CHANGE:
  258.         return CFileException::fileNotFound;
  259.     case ERROR_DRIVE_LOCKED:
  260.         return CFileException::lockViolation;
  261.     case ERROR_BUFFER_OVERFLOW:
  262.         return CFileException::badPath;
  263.     case ERROR_DISK_FULL:
  264.         return CFileException::diskFull;
  265.     case ERROR_NO_MORE_SEARCH_HANDLES:
  266.         return CFileException::tooManyOpenFiles;
  267.     case ERROR_INVALID_TARGET_HANDLE:
  268.         return CFileException::invalidFile;
  269.     case ERROR_INVALID_CATEGORY:
  270.         return CFileException::hardIO;
  271.     case ERROR_INVALID_NAME:
  272.         return CFileException::badPath;
  273.     case ERROR_INVALID_LEVEL:
  274.         return CFileException::badPath;
  275.     case ERROR_NO_VOLUME_LABEL:
  276.         return CFileException::badPath;
  277.     case ERROR_NEGATIVE_SEEK:
  278.         return CFileException::badSeek;
  279.     case ERROR_SEEK_ON_DEVICE:
  280.         return CFileException::badSeek;
  281.     case ERROR_DIR_NOT_ROOT:
  282.         return CFileException::badPath;
  283.     case ERROR_DIR_NOT_EMPTY:
  284.         return CFileException::removeCurrentDir;
  285.     case ERROR_LABEL_TOO_LONG:
  286.         return CFileException::badPath;
  287.     case ERROR_BAD_PATHNAME:
  288.         return CFileException::badPath;
  289.     case ERROR_LOCK_FAILED:
  290.         return CFileException::lockViolation;
  291.     case ERROR_BUSY:
  292.         return CFileException::accessDenied;
  293.     case ERROR_INVALID_ORDINAL:
  294.         return CFileException::invalidFile;
  295.     case ERROR_ALREADY_EXISTS:
  296.         return CFileException::accessDenied;
  297.     case ERROR_INVALID_EXE_SIGNATURE:
  298.         return CFileException::invalidFile;
  299.     case ERROR_BAD_EXE_FORMAT:
  300.         return CFileException::invalidFile;
  301.     case ERROR_FILENAME_EXCED_RANGE:
  302.         return CFileException::badPath;
  303.     case ERROR_META_EXPANSION_TOO_LONG:
  304.         return CFileException::badPath;
  305.     case ERROR_DIRECTORY:
  306.         return CFileException::badPath;
  307.     case ERROR_OPERATION_ABORTED:
  308.         return CFileException::hardIO;
  309.     case ERROR_IO_INCOMPLETE:
  310.         return CFileException::hardIO;
  311.     case ERROR_IO_PENDING:
  312.         return CFileException::hardIO;
  313.     case ERROR_SWAPERROR:
  314.         return CFileException::accessDenied;
  315.     default:
  316.         return CFileException::generic;
  317.     }
  318. }
  319.  
  320. #ifdef AFX_INIT_SEG
  321. #pragma code_seg(AFX_INIT_SEG)
  322. #endif
  323.  
  324. IMPLEMENT_DYNAMIC(CFileException, CException)
  325.  
  326. /////////////////////////////////////////////////////////////////////////////
  327.